package test.marui.com.mytest.view.slideBackView.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import test.marui.com.mytest.utils.CommonUtil;
import test.marui.com.mytest.utils.DisplayUtil;

/**
 * Created by MR on 2019/2/20.
 */
@SuppressLint("ViewConstructor")
public class SlideControlLayout extends FrameLayout {

    private final OnSlide onSlide;
    private ISlideView slideView;
    private int canSlideWidth;
    private boolean enable = true;
    private final SlideBackView slideBackView;
    private float downX;
    private boolean startDrag;
    private float moveX;


    public SlideControlLayout(@NonNull Context context, @IntRange(from = 10) int canSlideWidth, ISlideView slideView, OnSlide onSlide) {
        super(context);
        this.canSlideWidth = canSlideWidth;
        this.slideView = slideView;
        this.onSlide = onSlide;
        slideBackView = new SlideBackView(context, slideView);
        addView(slideBackView);
    }

    public SlideControlLayout attachToActivity(Activity activity) {
        ViewParent parent = getParent();
        if (parent instanceof ViewGroup) {
            ((ViewGroup) parent).removeView(this);
        }
        ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
        decor.addView(this, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return this;
    }

    // region 手势控制
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!enable) {
            return false;
        }

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (ev.getRawX() <= canSlideWidth) {
                return true;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!enable) {
            return super.onTouchEvent(event);
        }

        float currentX = event.getRawX();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float currentY = event.getRawY();
                if (currentY > DisplayUtil.dip2px(getContext(), 100) && currentX <= canSlideWidth) {
                    downX = currentX;
                    startDrag = true;
                    slideBackView.updateRate(0, false);
                    setSlideViewY(slideBackView, currentY);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (startDrag) {
                    moveX = currentX - downX;
                    if (Math.abs(moveX) <= slideBackView.getWidth() * 2) {
                        slideBackView.updateRate(moveX / 2, false);
                    } else {
                        slideBackView.updateRate(slideBackView.getWidth(), false);
                    }
                    setSlideViewY(slideBackView, event.getRawY());
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                if (startDrag && moveX >= slideBackView.getWidth() * 2) {
                    onBack();
                    slideBackView.updateRate(0, false);
                } else {
                    slideBackView.updateRate(0, startDrag);
                }
                startDrag = false;
                moveX = 0;
                break;
        }

        return startDrag || super.onTouchEvent(event);
    }

    private void onBack() {
        if (onSlide == null) {
            CommonUtil.getActivity(getContext()).onBackPressed();
        } else {
            onSlide.onSlideBack();
        }
    }

    private void setSlideViewY(SlideBackView view, float y) {
        if (!view.getSlideView().scrollVertical()) {
            scrollTo(0, 0);
            return;
        }
        scrollTo(0, (int) -(y - view.getHeight() / 2));
    }

    // endregion
}
